package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.JVcodeUser;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ForgetPasswordView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/18 0018.
 */

public class ForgetPasswordPresenter extends BasePresent<ForgetPasswordView> {

    RetrofitSerives retrofitSerives;
    Call<String> accountCall;

    public void getForgetPassword(String phone, String mcode) {
        JSONObject object = new JSONObject();
        try {
            object.put("phone", phone);
            object.put("mcode", mcode);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String requestStr = object.toString();

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddress);
        Call<JVcodeUser> call = retrofitSerives.getForgetPassword(body);

        call.enqueue(new Callback<JVcodeUser>() {
            @Override
            public void onResponse(Call<JVcodeUser> call, Response<JVcodeUser> response) {
                if (response.isSuccessful()) {
                    view.setResetPassword(response.body());
                } else {
                    view.onError("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<JVcodeUser> call, Throwable t) {
                view.onError("请求失败，请重试");
            }
        });
    }

    public void getIsAccountExists(String phone) {

        JSONObject object = new JSONObject();
        try {
            object.put("account", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = object.toString();
        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddress);
        accountCall = retrofitSerives.getIsAccountExists(body);

        accountCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    view.setIsAccountExists(response.body());
                } else {
                    view.onError("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                view.onError("请求失败，请重试");
            }
        });
    }

    public void getMesCode(String phone) {
        JSONObject object = new JSONObject();
        try {
            object.put("phone", phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = object.toString();

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddress);
        Call<String> call = retrofitSerives.getMesvcode(body);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    view.setMesCode(response.body());
                } else {
                    view.onError("请求失败，请重试");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                view.onError("请求服务器异常，稍候再试" + t.getMessage());
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
